import { INTEGER_PG } from "qqlx-core";

import { toNumber, toString } from "@lib/com.verify";

export class TransformerEnum<T> {
  ints!: T[];
  defaultEumn!: T;

  constructor(ints: (string | INTEGER_PG)[], defaultEumn: T) {
    this.ints = ints.filter((d) => typeof d === "number") as T[];
    this.defaultEumn = defaultEumn;
  }

  private trans(mess: any): T {
    const _mess = toNumber(mess) as T;
    const is_enum_valid = this.ints.includes(_mess);
    return is_enum_valid ? _mess : this.defaultEumn;
  }

  to(mess: any): T {
    return this.trans(mess);
  }

  from(mess: any): T {
    return this.trans(mess);
  }
}

export class TransformerEnumStr<T> {
  strs!: T[];
  defaultEumn!: T;

  constructor(strs: string[], defaultEumn: T) {
    this.strs = strs.filter((d) => typeof d === "string") as T[];
    this.defaultEumn = defaultEumn;
  }

  private trans(mess: any): T {
    const _mess = toString(mess) as T;
    const is_enum_valid = this.strs.includes(_mess);
    return is_enum_valid ? _mess : this.defaultEumn;
  }

  to(mess: any): T {
    return this.trans(mess);
  }

  from(mess: any): T {
    return this.trans(mess);
  }
}
